<?php

namespace App\Http\Form;

use App\Models\Common\ConfigModel;
use App\Support\CustomForm\Attribute\AttributeSupport;
use App\Support\CustomForm\AttributeWorkOrderSupport;
use Closure;
use Illuminate\Support\Facades\Cache;

class FormAttribute extends AttributeSupport
{
    /**
     * 图片展示闭包
     *
     * @return Closure
     */
    public static function getImgGroupSaveFunc(): Closure
    {
        return function ($value) {
            $value = is_array($value) ? $value : [];
            return array_map("getPrivateFileKey", $value);
        };
    }

    public function build(): array
    {
        AttributeWorkOrderSupport::doReject($this);

        return parent::build();
    }

    /**
     * 获取美团&饿了么 外卖建店承诺书&美食城列表
     * @param $type
     * @return array
     */
    public static function getWaimaiFormConfig($type): array
    {
        return Cache::remember(
            'getWaimaiFormConfig:' . $type, getSwitchCacheTime(1200), function () use ($type) {
            $value = ConfigModel::query()
                ->where('module', 'workOrder')
                ->where('name', 'waimaiFormConfig')
                ->value('value');

            $data = collect(json_decode($value, true) ?? [])
                ->where('type', $type)
                ->values()
                ->toArray();

            return $data[0]['data'] ?? [];
        }
        );
    }
}
